package com.cmos.controller;

import com.cmos.entity.Privilege;
import com.cmos.service.PrivilegeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("privilege")
public class PrivilegeController {
    @Resource
    private PrivilegeService privilegeService;

    //查看权限信息
    @RequestMapping("findById")
    public Map findById(String id){
        Map map = new HashMap();
        try {
            Privilege privilege = privilegeService.queryPrivilege(new Privilege().setId(id));
            map.put("status", 200);
            map.put("privilege", privilege);
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //查询所有权限
    @RequestMapping("findAllPrivilege")
    public Map findAllPrivilege(){
        Map map = new HashMap();
        try {
            List<Privilege> privileges = privilegeService.queryAllPrivilege();
            map.put("status", 200);
            map.put("privileges", privileges);
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //新增权限
    @RequestMapping("addPrivilege")
    public Map addPrivilege(Privilege privilege){
        Map map = new HashMap();
        try {
            String id = UUID.randomUUID().toString().replace("-", "");
            privilege.setId(id);
            privilegeService.savePrivilege(privilege);
            map.put("status", 200);
            map.put("message", "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //删除权限
    @RequestMapping("delPrivilege")
    public Map privilege(String id){
        Map map = new HashMap();
        try {
            privilegeService.removePrivilege(id);
            map.put("status", 200);
            map.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //修改权限信息
    @RequestMapping("updatePrivilege")
    public Map updatePrivilege(Privilege privilege){
        Map map = new HashMap();
        try {
            privilegeService.changePrivilege(privilege);
            map.put("status", 200);
            map.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }
}
